<?php

namespace App\Http\Controllers\Admin;

use App\Interfaces\SmsService;
use App\Models\Message;
use App\Models\User;
use Illuminate\Http\Request;
use App\Console\Commands\MassMessage;
use App\Http\Requests\Admin\Message\StoreRequest;
use App\Http\Requests\Admin\Message\UpdateRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Commands\OperationLogCommand;
class MessageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $message = Message::where('send_status',0)->paginate(10);
        //dd($message);

        //dd("dgagd");
        return view('admin.message.index',compact('message'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Message $message)
    {
        $message = Message::all();
        $send_object_arr = ['0'=>'所有用户','1'=>'近三个月登录','2'=>'邮箱认证'];

        return view('admin.message.create',compact('message','send_object_arr'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreRequest $request,Message $message)
    {
        //dd($request->all());
        $param=$request->all();
        if(mb_strlen($request->get('content'))>255){
            return redirect()->back()->withInput()->withErrors('短信字数不能超过255字');
        }

        $param['create_time']=date('Y-m-d H:i:s',time());
        //$content = $request->get('content');
        $message = $message->create($param);
        $this->dispatch(
            new OperationLogCommand('创建短信任务','Message','id:'.$message->id)
        );
        return Redirect('admin/message');

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $message = Message::find($id);
        $send_object_arr = ['0'=>'所有用户','1'=>'近三个月登录','2'=>'邮箱认证'];
        //dd($message);
       return view('admin.message.edit',compact('message','send_object_arr'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateRequest $request, $id)
    {
        $param = $request->all();
        if(mb_strlen($request->get('content'))>255){
            return redirect()->back()->withInput()->withErrors('短信字数不能超过255字');
        }
        //dd($param);
        $message = Message::find($id);
       // dd($message);
        $message->update($param);
        $this->dispatch(
            new OperationLogCommand('修改短信任务','Message','id:'.$id)
        );
        return Redirect('admin/message');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $return = ['status'=>0, 'message'=>'', 'data'=>''];
        $message = Message::find($id);
        $message->delete();
        $this->dispatch(
            new OperationLogCommand('删除短信任务','Message','id:'.$id)
        );
        return $return;
    }

    public function sendMessage(Request $request,SmsService $smsService){
        $return = ['status'=>0, 'message'=>'', 'data'=>''];
        $content = $request->get('content');
        $object = $request->get('object');
        $id = $request->get('id');

        $message = Message::find($id);

        $phone_arr = $smsService->getPhoneByObject($object);
        foreach ($phone_arr as $phone) {
            $phone_content[$phone] = $content;
        }

        $result = $smsService->sendMassMsg($phone_content);
        if($result){
            $return = ['status'=>1, 'message'=>'', 'data'=>''];
            $message->send_status=1;
            $message->send_time = date('Y-m-d H:i:s');
            $message->save();
        }
        $this->dispatch(
            new OperationLogCommand('发送短信','Message','id:'.$id)
        );
        return response()->json($return);
    }
}
